package com.pfzy.ui.activity.notification;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.KeyguardManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.PowerManager;
import android.provider.Settings;
import android.widget.RemoteViews;
import android.widget.Toast;

import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import com.pfzy.ui.MainActivity;
import com.pfzy.ui.R;
import com.pfzy.ui.utils.CommonUtil;

public class NotificationUtils extends ContextWrapper {
    private String CHANNEL_ID = "default";
    private String CHANNEL_NAME = "Default Channel";
    private int NOTIFICATION_ID = 9001; // 通知id
    private static NotificationUtils mInstance;
    private NotificationManager mManager;
    private NotificationCompat.Builder mBuilderProcess;
    private NotificationCompat.Builder mBuilderReceiver;
    private int mFlag = 0;
    public final String mStopAction = "com.pfzy.stop"; // 暂停继续action
    public final String mDoneAction = "com.pfzy.done"; // 完成action

    public NotificationUtils(Context context) {
        super(context);
    }

    public static NotificationUtils getInstance() {
        if (mInstance == null) {
            mInstance = new NotificationUtils(CommonUtil.getContext());
        }
        return mInstance;
    }

    /**
     * 普通通知
     */
    public void showNormal() {
        // 适配8.0及以上 创建渠道
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
            channel.setDescription("描述");
            channel.setShowBadge(false); // 是否在桌面显示角标
            getManager().createNotificationChannel(channel);
        }

        // 点击意图
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
        // 构建配置
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("普通通知") // 标题
                .setContentText("普通通知内容") // 文本
                .setSmallIcon(R.mipmap.ic_launcher) // 小图标
                .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher)) // 大图标
                .setPriority(NotificationCompat.PRIORITY_DEFAULT) // 7.0 设置优先级
                .setContentIntent(pendingIntent) // 跳转配置
                .setFullScreenIntent(pendingIntent, true) // 更新或创建通知,并注明通知的id
                .setChannelId(CHANNEL_ID)
                .setWhen(System.currentTimeMillis()) //通知产生的时间，会在通知信息里显示
                .setOngoing(false) //ture，设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
                .setDefaults(Notification.DEFAULT_ALL)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置，使用defaults属性，可以组合：
                .setAutoCancel(true); // 是否自动消失（点击）or mManager.cancel(mNormalNotificationId)、cancelAll、setTimeoutAfter()
        // 发起通知
        getManager().notify(NOTIFICATION_ID, builder.build());
    }

    /**
     * 重要通知
     */
    public void showHigh() {
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);// 在通知栏可见，有提示音，会横幅弹出
            channel.setShowBadge(true);
            getManager().createNotificationChannel(channel);
        }
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("重要通知")
                .setContentText("重要通知内容")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setFullScreenIntent(pendingIntent, true) // 更新或创建通知,并注明通知的id
                .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher))
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setDefaults(NotificationCompat.DEFAULT_ALL)  // 默认配置,包括通知的提示音,震动效果等
                .setNumber(999) // 自定义桌面通知数量
                .addAction(R.mipmap.ic_launcher, "去看看", pendingIntent)// 通知上的操作
                .setCategory(NotificationCompat.CATEGORY_MESSAGE) // 通知类别，"勿扰模式"时系统会决定要不要显示你的通知
                .setVisibility(NotificationCompat.VISIBILITY_PRIVATE); // 屏幕可见性，锁屏时，显示icon和标题，内容隐藏
        getManager().notify(NOTIFICATION_ID, builder.build());
    }

    /**
     * 进度条通知
     */
    public void showProgress() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            getManager().createNotificationChannel(channel);
        }

        int progressMax = 100;
        int progressCurrent = 30;
        mBuilderProcess = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("进度通知")
                .setContentText("下载中：" + progressCurrent)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher))
                // 第3个参数indeterminate，false表示确定的进度，比如100，true表示不确定的进度，会一直显示进度动画，直到更新状态下载完成，或删除通知
                .setProgress(progressMax, progressCurrent, false);
        getManager().notify(NOTIFICATION_ID, mBuilderProcess.build());
    }

    /**
     * 更新进度条通知
     * 1.更新进度：修改进度值即可
     * 2.下载完成：总进度与当前进度都设置为0即可，同时更新文案
     */
    public void updateProgress() {
        if (mBuilderProcess != null) {
            int progressMax = 100;
            int progressCurrent = 50;
            // 1.更新进度
            mBuilderProcess
                    .setContentText("下载中：" + progressCurrent)
                    .setProgress(progressMax, progressCurrent, false);
            // 2.下载完成
            //mBuilder.setContentText("下载完成！").setProgress(0, 0, false)
            getManager().notify(NOTIFICATION_ID, mBuilderProcess.build());
            Toast.makeText(this, "已更新进度到$progressCurrent%", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "请先发一条进度条通知", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 大文本通知
     */
    public void showBigText() {
        String bigText = "A notification is a message that Android displays outside your app's UI to provide the user with reminders, communication from other people, or other timely information from your app. Users can tap the notification to open your app or take an action directly from the notification.";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            getManager().createNotificationChannel(channel);
        }
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("大文本通知")
                .setStyle(new NotificationCompat.BigTextStyle().bigText(bigText))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher))
                .setAutoCancel(true);
        getManager().notify(NOTIFICATION_ID, builder.build());
    }

    /**
     * 大图片通知
     */
    public void showBigImage() {
        Bitmap bigPic = BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            getManager().createNotificationChannel(channel);
        }
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("大图片通知")
                .setContentText("有美女，展开看看")
                .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bigPic))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher))
                .setAutoCancel(true);
        getManager().notify(NOTIFICATION_ID, builder.build());
    }

    /**
     * 自定义通知
     */
    public void showCustom() {
        // 适配8.0及以上
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
            getManager().createNotificationChannel(channel);
        }

        // 适配12.0及以上
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
//            mFlag = PendingIntent.FLAG_IMMUTABLE;
//        } else {
        mFlag = PendingIntent.FLAG_UPDATE_CURRENT;
//        }

        // 添加自定义通知view
        RemoteViews views = new RemoteViews(getPackageName(), R.layout.layout_notification);

        // 添加暂停继续事件
        Intent intentStop = new Intent(mStopAction);
        PendingIntent pendingIntentStop = PendingIntent.getBroadcast(this, 0, intentStop, mFlag);
        views.setOnClickPendingIntent(R.id.btn_stop, pendingIntentStop);

        // 添加完成事件
        Intent intentDone = new Intent(mDoneAction);
        PendingIntent pendingIntentDone = PendingIntent.getBroadcast(this, 0, intentDone, mFlag);
        views.setOnClickPendingIntent(R.id.btn_done, pendingIntentDone);

        // 创建Builder
        mBuilderReceiver = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher))
                .setAutoCancel(true)
                .setCustomBigContentView(views)
                .setCustomHeadsUpContentView(views) //设置悬浮通知的布局
                .setCustomContentView(views) //设置收起后通知的布局
                .setCustomBigContentView(views) //设置展开后通知的布局
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setDefaults(NotificationCompat.DEFAULT_ALL)  // 默认配置,包括通知的提示音,震动效果等
                .setContent(views); //兼容低版本，可根据需求设置

        // 发起通知
        mManager.notify(NOTIFICATION_ID, mBuilderReceiver.build());
    }

    /**
     * 更新自定义通知
     */
    public void showUpdateCustom() {
        // 发送通知 更新状态及UI
        sendBroadcast(new Intent(mStopAction));
    }

    /**
     * 更新自定义通知View
     */
    public static Boolean mIsStop = false; // 是否在播放 默认未开始
    public void updateCustomView() {
        RemoteViews views = new RemoteViews(getPackageName(), R.layout.layout_notification);
        Intent intentUpdate = new Intent(NotificationUtils.getInstance().mStopAction);
        PendingIntent pendingIntentUpdate = PendingIntent.getBroadcast(this, 0, intentUpdate, mFlag);
        views.setOnClickPendingIntent(R.id.btn_stop, pendingIntentUpdate);
        // 根据状态更新UI
        if (mIsStop) {
            views.setTextViewText(R.id.tv_status, "那些你很冒险的梦-停止播放");
            views.setTextViewText(R.id.btn_stop, "继续");
        } else {
            views.setTextViewText(R.id.tv_status, "那些你很冒险的梦-正在播放");
            views.setTextViewText(R.id.btn_stop, "暂停");
        }

        mBuilderReceiver.setCustomContentView(views).setCustomBigContentView(views);
        // 重新发起通知更新UI，注意：必须得是同一个通知id，即mCustomNotificationId
        mManager.notify(NOTIFICATION_ID, mBuilderReceiver.build());
    }

    public void sendNotification(String title, String content) {
        NotificationCompat.Builder builder = showNotification();
        PendingIntent intent = getPendingIntent();
        builder.setContentIntent(intent);
        // 更新或创建通知,并注明通知的id
        builder.setFullScreenIntent(intent, true);
        getManager().notify(NOTIFICATION_ID, builder.build());

//        NotificationCompat.Builder builder = getNotification(title, content);
//        Notification notification = builder.build();
//        notification.flags = 2;
//        getManager().notify(NOTIFICATION_ID, notification);
    }

    /**
     * 是否打开通知按钮
     *
     * @param context
     * @return
     */
    public static boolean isNotificationEnabled(Context context) {
        return NotificationManagerCompat.from(context.getApplicationContext()).areNotificationsEnabled();
    }

    public static void openPush(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //这种方案适用于 API 26, 即8.0（含8.0）以上可以用
            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
            intent.putExtra(Settings.EXTRA_APP_PACKAGE, activity.getPackageName());
            intent.putExtra(Settings.EXTRA_CHANNEL_ID, activity.getApplicationInfo().uid);
            activity.startActivity(intent);
        } else {
            toPermissionSetting(activity);
        }
    }

    /**
     * 跳转到权限设置
     *
     * @param activity
     */
    public static void toPermissionSetting(Activity activity) {
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
            toSystemConfig(activity);
        } else {
            try {
                toApplicationInfo(activity);
            } catch (Exception e) {
                e.printStackTrace();
                toSystemConfig(activity);
            }
        }
    }

    /**
     * 应用信息界面
     *
     * @param activity
     */
    public static void toApplicationInfo(Activity activity) {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        localIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        localIntent.setData(Uri.fromParts("package", activity.getPackageName(), null));
        activity.startActivity(localIntent);
    }

    /**
     * 系统设置界面
     *
     * @param activity
     */
    public static void toSystemConfig(Activity activity) {
        try {
            Intent intent = new Intent(Settings.ACTION_SETTINGS);
            activity.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 展示通知栏
     */
    public NotificationCompat.Builder showNotification() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        // 通知产生的时间，会在通知信息里显示
        builder.setWhen(System.currentTimeMillis());
        // 设置此通知的相对优先级。
        builder.setPriority(NotificationCompat.PRIORITY_MAX);
        //ture，设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
        builder.setOngoing(false);
        builder.setTicker("正在播放");
        builder.setContentIntent(getDefaultIntent(Notification.FLAG_ONGOING_EVENT));

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
            mChannel.setDescription("通知栏");
            mChannel.enableLights(false);
            mChannel.setLightColor(Color.BLUE);
            mChannel.enableVibration(true);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            getManager().createNotificationChannel(mChannel);

            builder.setCustomBigContentView(getContentView(true))
                    .setCustomContentView(getContentView(false))
                    .setChannelId(mChannel.getId());
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            builder.setCustomBigContentView(getContentView(true))
                    .setCustomContentView(getContentView(false));
        } else {
            builder.setContent(getContentView(false));
        }

        return builder;
    }

    private void cancel() {
//        mNotificationManager.cancel(notificationId);//撤销指定id通知
    }

    private PendingIntent getDefaultIntent(int flags) {
        return PendingIntent.getActivity(this, 1, new Intent(), flags);
    }

    private PendingIntent getPendingIntent() {
        Intent resultIntent = new Intent(this, MainActivity.class);
        resultIntent.setAction(Intent.ACTION_MAIN);
        resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 2, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        return resultPendingIntent;
    }

    /**
     * 获取自定义通知栏view
     */
    private RemoteViews getContentView(boolean showBigView) {
        RemoteViews view = new RemoteViews(getPackageName(), showBigView ? R.layout.layout_notify_big : R.layout.layout_notify_small);
        return view;
    }

    private NotificationManager getManager() {
        if (this.mManager == null) {
            this.mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return this.mManager;
    }



//    private void mNotificationChannel(){
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//            if (mNotificationChannel == null) {
//                mNotificationChannel = new NotificationChannel(
//                        DOWNLOAD_CHANNEL_ID, DOWNLOAD_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
//                mNotificationChannel.setShowBadge(true);
//                mNotificationChannel.setLightColor(Color.BLUE);
//                mNotificationChannel.enableVibration(false);
//                notificationManager.createNotificationChannel(mNotificationChannel);
//            }
//            mNotification = new NotificationCompat.Builder(context, DOWNLOAD_CHANNEL_ID)
//                    .setWhen(System.currentTimeMillis())
//                    .setSmallIcon(R.mipmap.ic_launcher)
//                    .setPriority(NotificationCompat.PRIORITY_HIGH)
//                    .setDefaults(Notification.DEFAULT_ALL)
//                    .setOngoing(true)
//                    .setAutoCancel(true)
//                    .setContentTitle(title)
//                    .setContentText(content)
//                    .build();
//        }
//    }

//    private NotificationCompat.Builder getNotification(String title, String content) {
//        // 获取默认通知频道ID
//        NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), NotificationChannelUtil.getDefaultNotificationChannelId(getApplicationContext()));
//        // 设置此通知的相对优先级。
//        builder.setPriority(Notification.PRIORITY_DEFAULT);
//        // 上部标题
//        builder.setContentTitle(title);
//        // 中部通知内容
//        builder.setContentText(content);
//        // 左部图标
//        builder.setSmallIcon(R.mipmap.ic_launcher);
//        Intent intent = new Intent(this, RealOrderReceiver.class);
//        intent.putExtra(ConsIntentKey.AROUTER_NAME_ORDER, this.mOrder);
//        intent.putExtra(ConsIntentKey.AROUTER_NAME_CMDMSG, this.mMsg);
//        intent.putExtra(ConsIntentKey.AROUTER_SHOW_NOTIFICATION_TIME, this.mTime);
//        intent.putExtra(ConsIntentKey.AROUTER_FROM_NOTIFICATION_CLICK, true);
//        PendingIntent pendingIntentClick = PendingIntent.getBroadcast(this, this.mOrder.getOrderNo(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
//        builder.setContentIntent(pendingIntentClick);
//        return builder;
//    }
//
//    private void createNotificationChannel() {
//        NotificationChannel channel = new NotificationChannel("default", CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
//        channel.canBypassDnd();
//        channel.enableLights(true);
//        channel.setLockscreenVisibility(-1);
//        channel.setLightColor(SupportMenu.CATEGORY_MASK);
//        channel.canShowBadge();
//        channel.enableVibration(true);
//        channel.getAudioAttributes();
//        channel.getGroup();
//        channel.setBypassDnd(true);
//        channel.setVibrationPattern(new long[]{100, 100, 200});
//        channel.shouldShowLights();
//        getManager().createNotificationChannel(channel);
//    }

    //    private static final String CHANNEL_DESCRIPTION = "this is default channel!";
//    private static final int NOTIFICATION_ID = 100001;
//    private static RealOrderNotificationUtils mRealOrderNotificationUtils;
//    private Handler mHandler;
//    private Msg mMsg;
//    private Order mOrder;
//    private long mTime;
//
//    public static RealOrderNotificationUtils getIntance() {
//        if (mRealOrderNotificationUtils == null) {
//            mRealOrderNotificationUtils = new RealOrderNotificationUtils(Common.getContext());
//        }
//        return mRealOrderNotificationUtils;
//    }
//
//    private RealOrderNotificationUtils(Context base) {
//        super(base);
//    }
//
//    @TargetApi(26)
//    private void createNotificationChannel() {
//        NotificationChannel channel = new NotificationChannel("default", CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
//        channel.canBypassDnd();
//        channel.enableLights(true);
//        channel.setLockscreenVisibility(-1);
//        channel.setLightColor(SupportMenu.CATEGORY_MASK);
//        channel.canShowBadge();
//        channel.enableVibration(true);
//        channel.getAudioAttributes();
//        channel.getGroup();
//        channel.setBypassDnd(true);
//        channel.setVibrationPattern(new long[]{100, 100, 200});
//        channel.shouldShowLights();
//        getManager().createNotificationChannel(channel);
//    }
//


//    /**
//     * 取消通知
//     */
//    public void cancelNotifaction() {
//        if (AppConfig.getNotificationSwitch()) {
//            if (this.mHandler != null && this.mOrder.getConfirmWay() == 0) {
//                this.mHandler.removeCallbacksAndMessages(null);
//            }
//            getManager().cancel(NOTIFICATION_ID);
//            this.mOrder = null;
//            this.mMsg = null;
//            AppConfig.setConfirmOrder("");
//            AppConfig.setConfirmOrderMsg("");
//        }
//    }
//
//    /**
//     * 按订单号取消通知
//     *
//     * @param orderNo
//     */
//    public void cancelNotifactionByOrderNo(long orderNo) {
//        if (AppConfig.getNotificationSwitch() && this.mOrder != null && this.mOrder.getOrderNo() == orderNo) {
//            if (this.mHandler != null && this.mOrder.getConfirmWay() == 0) {
//                this.mHandler.removeCallbacksAndMessages(null);
//            }
//            // 语音停止
//            UXSpeaksManager.getInstance().stop();
//            UXSpeaksManager.getInstance().stopLocalMedia();
//            // 取消通知ID
//            getManager().cancel(NOTIFICATION_ID);
//            this.mOrder = null;
//            this.mMsg = null;
//            // 设置确认订单
//            AppConfig.setConfirmOrder("");
//            AppConfig.setConfirmOrderMsg("");
//            HashMap<String, String> showMap = new HashMap<>();
//            showMap.put("order", Long.valueOf(orderNo).toString());
//            SendDataUtil.click("D171676", null, showMap);
//        }
//    }
//
//    public void sendRealOrderNotification(String title, String content, Order order, Msg msg) {
//        try {
//            if (!OrderLabelUtils.isContainGivenLabel(order.getOrderLabel(), OrderLabelEnum.CARPOOL_ORDER) && AppConfig.getNotificationSwitch()) {
//                wakeUpAndUnlock(Common.getContext());
//                HashMap showMap = new HashMap();
//                showMap.put("order", Long.valueOf(order.getOrderNo()));
//                SendDataUtil.show("D171673", null, showMap);
//                this.mOrder = order;
//                AppConfig.setConfirmOrder(JSONObject.toJSONString(order));
//                AppConfig.setConfirmOrderMsg(JSONObject.toJSONString(msg));
//                this.mMsg = msg;
//                this.mTime = System.currentTimeMillis();
//                if (this.mHandler == null) {
//                    this.mHandler = new Handler();
//                }
//                this.mHandler.removeCallbacksAndMessages(null);
//                if (this.mOrder != null && this.mOrder.getConfirmWay() == 0) {
//                    this.mHandler.postDelayed(new Runnable() { // from class: com.pfzy.driver.driver_home.module.home.RealOrderNotificationUtils.1
//                        @Override // java.lang.Runnable
//                        public void run() {
//                            RealOrderNotificationUtils.this.cancelNotifaction();
//                            try {
//                                HashMap<String, String> customMap = SendDataUtil.getCustomMap();
//                                SendDataUtil.click("D171678", null, customMap);
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                            }
//                        }
//                    }, 16000L);
//                }
//                NotificationCompat.Builder builder = getNotification(title, content);
//                Notification notification = builder.build();
//                notification.flags = 2;
//                getManager().notify(NOTIFICATION_ID, notification);
//                if (order.getConfirmWay() == 0) {
//                    if (OrderLabelUtils.isContainGivenLabel(order.getOrderLabel(), OrderLabelEnum.RELAY_ORDER)) {
//                        DriverUXSpeakUtil.playLocalMedia(R.raw.relay, 6, 5);
//                    } else if (OrderLabelUtils.isContainGivenLabel(order.getOrderLabel(), OrderLabelEnum.CARPOOL_ORDER)) {
//                        DriverUXSpeakUtil.speak("您有一个拼车单，请尽快接单,您有一个拼车单，请尽快接单,您有一个拼车单，请尽快接单,您有一个拼车单，请尽快接单,您有一个拼车单，请尽快接单,您有一个拼车单，请尽快接单,您有一个拼车单，请尽快接单", 6.0d);
//                    } else {
//                        DriverUXSpeakUtil.playLocalMedia(R.raw.notificationrealtime, 6, 5);
//                    }
//                } else if (OrderLabelUtils.isContainGivenLabel(order.getOrderLabel(), OrderLabelEnum.RELAY_ORDER)) {
//                    DriverUXSpeakUtil.playLocalMedia(R.raw.autorelay, 6, false);
//                } else if (OrderLabelUtils.isContainGivenLabel(order.getOrderLabel(), OrderLabelEnum.CARPOOL_ORDER)) {
//                    DriverUXSpeakUtil.speak("您有一个拼车单已接单，请尽快前往服务！您有一个拼车单已接单，请尽快前往服务！您有一个拼车单已接单，请尽快前往服务！", 6.0d);
//                    L.e("RealOrderReceiver", "speak您有一个拼车单已接单，请尽快前往服务！您有一个拼车单已接单，请尽快前往服务！您有一个拼车单已接单，请尽快前往服务！");
//                } else {
//                    DriverUXSpeakUtil.playLocalMedia(R.raw.notificationrealtime, 6, 5);
//                }
//            }
//        } catch (Exception e) {
//            L.e("RealOrderReceiver", e);
//            e.printStackTrace();
//        }
//    }
//
//    private NotificationCompat.Builder getNotification(String title, String content) {
//        NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), NotificationChannelUtil.getDefaultNotificationChannelId(getApplicationContext()));
//        builder.setPriority(0);
//        builder.setContentTitle(title);
//        builder.setContentText(content);
//        builder.setSmallIcon(R.mipmap.ic_launcher);
//        Intent intent = new Intent(this, RealOrderReceiver.class);
//        intent.putExtra(ConsIntentKey.AROUTER_NAME_ORDER, this.mOrder);
//        intent.putExtra(ConsIntentKey.AROUTER_NAME_CMDMSG, this.mMsg);
//        intent.putExtra(ConsIntentKey.AROUTER_SHOW_NOTIFICATION_TIME, this.mTime);
//        intent.putExtra(ConsIntentKey.AROUTER_FROM_NOTIFICATION_CLICK, true);
//        PendingIntent pendingIntentClick = PendingIntent.getBroadcast(this, (int) this.mOrder.getOrderNo(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
//        builder.setContentIntent(pendingIntentClick);
//        return builder;
//    }
//
//    /**
//     * 发送实时订单广播
//     */
//    public void sendBroadcastForRealOrder() {
//        if (AppConfig.getNotificationSwitch()) {
//            Order order = null;
//            Msg msg = null;
//            if (this.mMsg != null) {
//                msg = this.mMsg;
//                order = JSONObject.parseObject(msg.getContent(), Order.class);
//            }
//            if (this.mOrder != null) {
//                order = this.mOrder;
//            }
//            try {
//                if (this.mOrder == null && !TextUtils.isEmpty(AppConfig.getConfirmOrder())) {
//                    order = JSONObject.parseObject(AppConfig.getConfirmOrder(), Order.class);
//                    AppConfig.setConfirmOrder("");
//                }
//                if (this.mMsg == null && !TextUtils.isEmpty(AppConfig.getConfirmOrderMsg()) && !"null".equals(AppConfig.getConfirmOrderMsg())) {
//                    msg = JSONObject.parseObject(AppConfig.getConfirmOrderMsg(), Msg.class);
//                    if (order == null) {
//                        order = JSONObject.parseObject(msg.getContent(), Order.class);
//                    }
//                    AppConfig.setConfirmOrderMsg("");
//                }
//                if (!ConstsValue.inRealOrderPage && order != null) {
//                    getIntance().cancelNotifaction();
//                    if (order.getConfirmWay() == 0) {
//                        if (msg != null) {
//                            UXRouter.url(ConstsPath.ORDERCENTER_MAIN)
//                                    .withBoolean(ConsIntentKey.AROUTER_REALORDER_NOTIFICATION_CANCEL, false)
//                                    .withParcelable(ConsIntentKey.AROUTER_NAME_CMDMSG, msg)
//                                    .withBoolean(ConsIntentKey.AROUTER_FROM_ORDER_NOTIFICATION, true)
//                                    .withTransition(R.anim.anim_start_enter_alpha, R.anim.anim_start_exit)
//                                    .navigation(CaocaoActivityManager.getInstance().getCurrentActivity());
//                        } else {
//                            return;
//                        }
//                    } else {
//                        EventBus.getDefault().postSticky(new EventBusFinishRealOrder(order.getOrderNo()));
//                        UXRouter.url(ConstsPath.ORDERCENTER_AUTOCONFIRM).withSerializable(ConsIntentKey.AROUTER_NAME_ORDER, order).withBoolean(ConsIntentKey.AROUTER_NAME_SPEAK, true).withBoolean(ConsIntentKey.AROUTER_FROM_ORDER_NOTIFICATION, true).withTransition(R.anim.no_anim, R.anim.no_anim).navigation(CaocaoActivityManager.getInstance().getCurrentActivity());
//                    }
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//    }
//
//    /**
//     * 唤醒和解锁
//     *
//     * @param context
//     */
//    public void wakeUpAndUnlock(Context context) {
//        try {
//            KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
//            KeyguardManager.KeyguardLock kl = km.newKeyguardLock("unLock");
//            kl.disableKeyguard();
//            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
//            PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "com.pfzy.driver:bright");
//            wl.acquire();
//            wl.release();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
}
